package com.penghaisoft.microwms.material.web.insidewarehouse.service.impl;

import java.util.Date;
import java.util.HashSet;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.penghaisoft.microwms.common.Constant;
import com.penghaisoft.microwms.common.dto.Resp;
import com.penghaisoft.microwms.common.page.Pager;
import com.penghaisoft.microwms.common.service.BaseService;
import com.penghaisoft.microwms.common.wms.model.MSBarCode;
import com.penghaisoft.microwms.common.wms.model.RawContractChange;
import com.penghaisoft.microwms.common.wms.model.RawContractChangePlan;
import com.penghaisoft.microwms.material.web.insidewarehouse.dao.RawContractChangeMapper;
import com.penghaisoft.microwms.material.web.insidewarehouse.dao.RawContractChangePlanMapper;
import com.penghaisoft.microwms.material.web.insidewarehouse.service.IRawContractChangePlanService;
import com.penghaisoft.microwms.material.web.intowarehouse.dao.MSBarCodeMapper;

/**
 * @Package com.penghaisoft.microwms.material.web.insidewarehouse.service.impl
 * @ClassName: RawContractChangePlanServiceImpl
 * @Description: 原材料合同变更计划表
 * @author luoteng
 * @date 2019年3月6日 上午9:33:00
 */
@Service("rawContractChangePlanService")
public class RawContractChangePlanServiceImpl extends BaseService implements IRawContractChangePlanService {
	@Resource
	private RawContractChangePlanMapper rawContractChangePlanMapper;
	@Resource
	private MSBarCodeMapper mSBarCodeMapper;
	
	@Resource
	private RawContractChangeMapper rawContractChangeMapper;

	/**
	 * 新增记录
	 * 
	 * @param baseResource
	 * @return
	 */
	@Override
	public Resp create(RawContractChangePlan rawContractChangePlan) {
		rawContractChangePlanMapper.create(rawContractChangePlan);
		return success();
	}

	/**
	 * 删除记录（逻辑删除）
	 * 
	 * @param baseResource
	 * @return
	 */
	@Override
	public Resp delete(RawContractChangePlan rawContractChangePlan) {
		rawContractChangePlanMapper.delete(rawContractChangePlan);
		return success();
	}

	/**
	 * 查询列表
	 * 
	 * @param pager
	 * @param baseResource
	 * @return
	 */
	@Override
	public Pager<RawContractChangePlan> findListByCondition(int page, int rows, RawContractChangePlan condition) {
		Pager<RawContractChangePlan> pager = new Pager<>();
		pager.setPage(page);
		pager.setRows(rows);
		condition.preQuery();
		List<RawContractChangePlan> records = rawContractChangePlanMapper.queryList(pager, condition);
		long size = rawContractChangePlanMapper.queryCount(condition);
		pager.setRecords(records);
		pager.setTotalCount(size);
		return pager;
	}

	/**
	 * 查询单条
	 * 
	 * @param id
	 * @return
	 */
	@Override
	public RawContractChangePlan findById(String id) {
		return rawContractChangePlanMapper.queryById(id);
	}

	/**
	 * 修改记录
	 * 
	 * @param baseResource
	 * @return
	 */
	@Override
	public Resp update(RawContractChangePlan rawContractChangePlan) {
		rawContractChangePlanMapper.updateBySelect(rawContractChangePlan);
		return success();
	}
	
	/**
	 * 修改记录
	 * 
	 * @param baseResource
	 * @return
	 */
	@Override
	public Resp updateNew(RawContractChangePlan rawContractChangePlan) {
		rawContractChangePlanMapper.updateBySelectNew(rawContractChangePlan);
		return success();
	}

	@Override
	public Resp updateByPlanNo(RawContractChangePlan rawContractChangePlan) {
		rawContractChangePlanMapper.updateByPlanNo(rawContractChangePlan);
		return success();
	}

	@Override
	public List<RawContractChangePlan> queryListByAny(RawContractChangePlan condition) {
		return rawContractChangePlanMapper.queryByAny(condition);
	}

	@Override
	@Transactional
	public Resp insert(List<RawContractChangePlan> list, String contractChangePlanNo, String loginName) {
		Date now = new Date();
		HashSet<String> allSet = new HashSet<String>();
		for (int i = 0; i < list.size(); i++) {
			RawContractChangePlan tmp = list.get(i);

			StringBuffer uniqueStr = new StringBuffer("");
			uniqueStr.append(tmp.getModel());
			uniqueStr.append(tmp.getModelType());
			if (allSet.contains(uniqueStr.toString())) {
				Resp resp = new Resp();
				resp.setCode(Constant.FAIL);
				resp.setMsg("选择的型号、型号类型重复");
				return resp;
			} else {
				allSet.add(uniqueStr.toString());
			}
		}

		for (int i = 0; i < list.size(); i++) {
			RawContractChangePlan rawContractChangePlan = list.get(i);

//	    		原合同编号、新合同编号、型号、型号类型、需求数量、是否需要归还 页面传递的参数已经提供
			rawContractChangePlan.setContractChangePlanNo(contractChangePlanNo);
			rawContractChangePlan.setActiveFlag("1");
			rawContractChangePlan.setGmtCreate(now);
			rawContractChangePlan.setCreateBy(loginName);
			//打印状态默认为0
			rawContractChangePlan.setUserDefined2("0");
//				是否需要归还： 1：需要 2：不需要
			if (rawContractChangePlan.getBackFlag() != null && "1".equals(rawContractChangePlan.getBackFlag())) {
//					归还状态： 0：未归还 1：部分归还 2：已经归还
				rawContractChangePlan.setBackStatus("0");
			}
//				状态：0：创建 1：通过 2：驳回 3：扫码中 4：扫码完成 5：确认完成
			rawContractChangePlan.setStatus("0");

			rawContractChangePlanMapper.create(rawContractChangePlan);
		}

		return success();
	}

	@Override
	@Transactional
	public Resp backUpdate(RawContractChangePlan rawContractChangePlan, String contractChangePlanNo, String loginName) {
		RawContractChangePlan ob = new RawContractChangePlan();
		ob.setActiveFlag("1");
		ob.setRawContractChangePlanId(rawContractChangePlan.getRawContractChangePlanId());

		List<RawContractChangePlan> list = rawContractChangePlanMapper.queryByAny(ob);
		if (list != null && !list.isEmpty()) {
			RawContractChangePlan tmp = list.get(0);

			RawContractChangePlan newPlan = new RawContractChangePlan();
			newPlan.setRelationPlanId(rawContractChangePlan.getRawContractChangePlanId());

//	    		原合同编号、新合同编号、型号、型号类型、需求数量、是否需要归还 页面传递的参数已经提供
			newPlan.setContractNoOld(rawContractChangePlan.getContractNoOld());
			newPlan.setContractNoNew(rawContractChangePlan.getContractNoNew());
			newPlan.setModel(rawContractChangePlan.getModel());
			newPlan.setModelType(rawContractChangePlan.getModelType());
			newPlan.setModelDesc(rawContractChangePlan.getModelDesc());
			newPlan.setRequireAmount(rawContractChangePlan.getRequireAmount());

			newPlan.setUnit(tmp.getUnit());

//				状态：0：创建 1：通过 2：驳回 3：扫码中 4：扫码完成 5：确认完成
			newPlan.setStatus("0");
			newPlan.setContractChangePlanNo(contractChangePlanNo);
			newPlan.setActiveFlag("1");
			newPlan.setGmtCreate(new Date());
			newPlan.setCreateBy(loginName);
//				变更类型：1：借出 2：归还
			newPlan.setChangeType("2");

			rawContractChangePlanMapper.create(newPlan);
		}

		return success();
	}

	@Override
	public List<RawContractChangePlan> getJYHZ(RawContractChangePlan condition) {
		return rawContractChangePlanMapper.getJYHZ(condition);
	}

	/**
	 * 查询列表
	 * 
	 * @param pager
	 * @param baseResource
	 * @return
	 */
	@Override
	public Pager<RawContractChangePlan> getJYHZInfo(int page, int rows, RawContractChangePlan condition) {
		Pager<RawContractChangePlan> pager = new Pager<>();
		pager.setPage(page);
		pager.setRows(rows);
		condition.preQuery();
		List<RawContractChangePlan> records = rawContractChangePlanMapper.queryList(pager, condition);
		long size = rawContractChangePlanMapper.queryCount(condition);
		pager.setRecords(records);
		pager.setTotalCount(size);
		return pager;
	}
	
	/**  
	 * @Title: makeNewBarcode
	 * @Description:  合同借用管理重新生成条码 
	 * @author Lengxf  
	 * @date 2019年3月26日    
	 */  
	@Override
	@Transactional
	public Resp makeNewBarcode(RawContractChangePlan rawContractChangePlan){
		Date now = new Date();
		Resp resp = new Resp();

		boolean printfFlag = false;
		
		List<RawContractChangePlan> listIsPrintfOrNo = rawContractChangePlanMapper.queryByAny(rawContractChangePlan);
		if(listIsPrintfOrNo != null && listIsPrintfOrNo.size() > 0){
			RawContractChangePlan isPrintfOrNo = listIsPrintfOrNo.get(0);
			//判断是否打印 0：未打印 1：已打印
			if(isPrintfOrNo.getUserDefined2() != null && isPrintfOrNo.getUserDefined2().equals("1")){
				resp.setCode(Constant.FAIL);
				resp.setMsg("该借用合同下的该物料的条码已打印！");
				return resp;
			}else{
				printfFlag = true;
			}
		}
		
		if(printfFlag){
			RawContractChange entity = new RawContractChange();
			entity.setContractNoNew(rawContractChangePlan.getContractNoNew());
			entity.setModel(rawContractChangePlan.getModel());
			List<RawContractChange> records = rawContractChangeMapper.queryByAny(entity);
			MSBarCode msBarCode = new MSBarCode();
			if(records != null && records.size() > 0){
				Integer num =0;
				for(RawContractChange tmp : records){
					msBarCode.setBarCode(tmp.getBarcode());
					msBarCode.setModelCode(tmp.getModel());
					msBarCode.setModelType(tmp.getModelType());
					msBarCode.setContractNo(tmp.getContractNoNew());
					msBarCode.setUpdateDate(now);
					msBarCode.setBatchCode(rawContractChangePlan.getContractChangePlanNo());
//					产品类型：0：原材料 1：成品
					msBarCode.setProductType("0");
//					条码打印接口表
					mSBarCodeMapper.insertSelective(msBarCode);
					num++;
				}
				
				//打印状态改为已经生成
				rawContractChangePlan.setUserDefined2("1");
				rawContractChangePlanMapper.updateByModelNo(rawContractChangePlan);
				
				resp.setCode(Constant.SUCCESS);
				resp.setMsg("重新生成了" + num + "个条码，请在打印机进行打印~");
			}else{
				resp.setCode(Constant.FAIL);
				resp.setMsg("参数传递错误");
				return resp;
			}
		}

		return resp;
	}
}
